<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basicInformation:equipment:add']"
        >新增
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['basicInformation:equipment:edit']"-->
<!--        >修改-->
<!--        </el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basicInformation:equipment:remove']"
        >批量删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basicInformation:equipment:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="equipmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主建id" align="center" prop="id" v-if="false"/>
      <el-table-column label="设备名称" align="center" prop="name"/>
      <el-table-column label="规格型号" align="center" prop="type"/>
      <el-table-column label="制造厂家" align="center" prop="manufactor"/>
      <el-table-column label="出厂年月" align="center" prop="deliveryDate" width="180">
        <template slot-scope="scope">
        </template>
      </el-table-column>
      <el-table-column label="出厂编号" align="center" prop="deliveryCode"/>
      <el-table-column label="功率" align="center" prop="power"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="所属船只编码" align="center" prop="shipCode"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basicInformation:equipment:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basicInformation:equipment:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改通导设备信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入设备名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格型号" prop="type">
              <el-input v-model="form.type" placeholder="请输入规格型号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="制造厂家" prop="manufactor">
              <el-input v-model="form.manufactor" placeholder="请输入制造厂家"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出厂年月" prop="deliveryDate">
              <el-date-picker clearable size="small"
                              style="width: 250px;"
                              v-model="form.deliveryDate"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              :picker-options="pickerOptions"
                              placeholder="选择出厂年月">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="出厂编号" prop="deliveryCode">
              <el-input v-model="form.deliveryCode" placeholder="请输入出厂编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="功率" prop="power">
              <el-input v-model="form.power" placeholder="请输入功率"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">

            <el-form-item label="船舶编码" prop="shipCode">
              <el-input v-model="form.shipCode" readonly style="width:280px" />
              <el-button type="primary" v-model="form.shipId" @click="selectShip" :disabled="stateShow">选择船只</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="titleShip" :visible.sync="openShip" width="800px" append-to-body>
      <el-form :model="queryShipParams" ref="queryShipForm" :inline="true" v-show="showSearchShip" label-width="68px">
        <el-form-item label="船名" prop="shipName">
          <el-input
            v-model="queryShipParams.shipName"
            placeholder="请输入船名"
            clearable
            size="small"
            @keyup.enter.native="handleQueryShip"
          />
        </el-form-item>
        <el-form-item label="船舶编码" prop="shipCode">
          <el-input
            v-model="queryShipParams.shipCode"
            placeholder="请输入船舶编码"
            clearable
            size="small"
            @keyup.enter.native="handleQueryShip"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryShip">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetShipQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="shipList" @selection-change="handleSelectionChangeShip">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="主建id" align="center" prop="id" v-if="false"/>
        <el-table-column label="船名" align="center" prop="shipName"/>
        <el-table-column label="船舶编码" align="center" prop="shipCode"/>
      </el-table>
      <pagination
        v-show="totalShip>0"
        :total="totalShip"
        :page.sync="queryShipParams.pageNum"
        :limit.sync="queryShipParams.pageSize"
        @pagination="getShipList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="okShip">确 定</el-button>
        <el-button @click="cancelShip">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listEquipment,
    getEquipment,
    delEquipment,
    addEquipment,
    updateEquipment,
    exportEquipment
  } from '@/api/basicInformation/equipment'

  import { listShip } from '@/api/basicInformation/ship'

  export default {
    name: 'Equipment',
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        names: [],
        idsShip: [],
        shipCodes: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 显示搜索条件
        showSearchShip: true,
        // 总条数
        total: 0,
        totalShip: 0,
        // 通导设备信息表格数据
        equipmentList: [],
        shipList: [],
        // 弹出层标题
        title: '',
        titleShip: '选择船只',
        // 是否显示弹出层
        open: false,
        openShip: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          orderByColumn: 'id',
          isAsc: 'asc'
        },
        queryShipParams: {
          pageNum: 1,
          pageSize: 10,
          shipName: undefined,
          shipCode: undefined
        },
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() >= (Date.now()-8.64e7);
          }
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            { required: true, message: "设备名称不能为空", trigger: "change" }
          ],
          type: [
            { required: true, message: "规格型号不能为空", trigger: "change" }
          ],
          shipCode: [
            { required: true, message: "船舶编码不能为空", trigger: "change" }
          ]
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询通导设备信息列表 */
      getList() {
        this.loading = true
        listEquipment(this.queryParams).then(response => {
          this.equipmentList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      getShipList() {
        this.loading = true
        listShip(this.queryShipParams).then(response => {
          this.shipList = response.rows
          this.totalShip = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      okShip() {
        if (this.idsShip.length > 1) {
          this.msgInfo('只能绑定一条船只')
        } else if (this.idsShip.length === 1) {
          this.form.shipId = this.idsShip[0]
          this.form.shipCode = this.shipCodes[0]
          this.openShip = false
        } else {
          this.msgInfo('请选择船只')
        }
      },
      cancelShip() {
        this.openShip = false
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          name: undefined,
          type: undefined,
          manufactor: undefined,
          deliveryDate: undefined,
          deliveryCode: undefined,
          power: undefined,
          remark: undefined,
          shipId: undefined,
          delFlag: undefined,
          createById: undefined,
          createBy: undefined,
          createTime: undefined,
          updateById: undefined,
          updateBy: undefined,
          updateTime: undefined
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      handleQueryShip() {
        this.queryShipParams.pageNum = 1
        this.getShipList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      resetShipQuery() {
        this.resetForm('queryShipForm')
        this.handleQueryShip()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.names = selection.map(item => item.name)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      // 多选框选中数据
      handleSelectionChangeShip(selection) {
        this.idsShip = selection.map(item => item.id)
        this.shipCodes = selection.map(item => item.shipCode)
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加通导设备信息'
      },
      selectShip() {
        this.openShip = true
        this.handleQueryShip()
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        getEquipment(id).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改通导设备信息'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateEquipment(this.form).then(response => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addEquipment(this.form).then(response => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        const names = row.name || this.names
        this.$confirm('是否确认删除设备名称为"' + names + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delEquipment(ids)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有通导设备信息数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return exportEquipment(queryParams)
        }).then(response => {
          this.download(response.msg)
        })
      }
    }
  }
</script>
